package com.dfk.bs.controller;


import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfk.bs.common.config.websocket.WebSocketServer;
import com.dfk.bs.common.constants.ResultCodeEnum;
import com.dfk.bs.common.exception.BsException;
import com.dfk.bs.common.vo.R;
import com.dfk.bs.dao.user.MessageMapper;
import com.dfk.bs.entity.Housetype;
import com.dfk.bs.entity.vo.Message;
import com.dfk.bs.service.user.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Api("发送信息管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/ddkbishe/message")
public class MessageController {

    @Autowired
    MessageService messageService;

    @Autowired
    MessageMapper messageMapper;


    @ApiOperation("查询所有信息")
    @GetMapping("/listAll")
    public R listAll() {
        List<Message> messages = messageMapper.selectAll();
        return R.ok()
                .message("查询最新四条信息成功")
                .data("items", messages);
    }


    @ApiOperation("发送信息")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "message", value = "信息", required = true)
            @RequestBody Message message) {
        messageService.save(message);
        return R.ok()
                .message("发送信息成功");
    }


    @ApiOperation("根据id删除信息")
    @DeleteMapping("/remove/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "id", required = true)
            @PathVariable String id) {
        messageService.removeById(id);

        return R.ok()
                .message("删除信息成功");
    }

    @ApiOperation("分页条件查询信息列表")
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody Map<String, Object> params) {

        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");

        String selectname = (String) params.get("selectname");

        //业务逻辑错误，抛出自定义异常
        if (page <= 0 || limit <= 0) {
            throw new BsException(ResultCodeEnum.PARAM_ERROR);
        }
        Page<Message> pageParam = new Page<>(page, limit);

        /*已经把查询的每一页的信息放到 pageParam中*/
        messageService.pageQuery(pageParam, selectname);

        List<Message> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok()
                .message("分页条件查询信息列表成功")
                .data("total", total)
                .data("rows", records);
    }

    @ApiOperation(value = "根据ID修改信息")
    @PutMapping("/update")
    public R updateById(
            @ApiParam(name = "message", value = "信息对象", required = true)
            @RequestBody Message message) {
        messageService.updateById(message);
        return R.ok()
                .message("修改信息成功");
    }


    @ApiOperation("查询业主所有信息")
    @PostMapping("/list/{housecode}")
    public R listBycode(@ApiParam(name = "housecode", value = "housecode", required = true)
                        @PathVariable String housecode) {
        List<Message> messageList = messageService.list();
        Collections.reverse(messageList);
        return R.ok()
                .message("查询业主信息成功")
                .data("items", messageList);
    }

    @ApiOperation("发送消息")
    @PostMapping("/push")
    public R pushToWeb(@RequestBody JSONObject json) throws IOException {
        String title = json.getString("title");
        String content = json.getString("content");


        String housecodelist = json.getString("housecodelist");
        String substring = housecodelist.substring(1, housecodelist.length() - 1);
        System.out.println("substring"+substring);
        String[] strings = substring.split(",");
        for (String s : strings) {
            Message message=new Message();
            message.setContent(content);
            message.setTitle(title);
            message.setHousecode(s);
            messageService.save(message);
//           WebSocketServer.sendInfo(message,s);
        }
        return R.ok().message("消息发送成功");
    }


    /**
     * @Function: 短信验证接口
     * @author: Yangxf
     * @Date: 2019/4/11 15:39
     */
    @RequestMapping("/smsverification")
    public Object SmsVerification(@Param("phone") String phone) {
        return messageService.SmsVerification(phone);
    }


}
